package org.teachertools.web.actions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.teachertools.data.Matiere;
import org.teachertools.services.MatiereService;
import org.teachertools.web.forms.MatiereForm;
import org.teachertools.web.forms.MatiereFormMapping;

public class MatiereAction extends CommonDispatchAction {

    @Override
    protected Map<String, String> getKeyMethodMap() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("button.save", "save");
        map.put("button.delete", "delete");
        map.put("button.search", "search");
        return map;
    }

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        MatiereForm matiereForm = (MatiereForm) form;
        MatiereService matiereService = this.getServiceLocator().getMatiereService();
        boolean isUpdate = matiereForm.getIsUpdate();
        if (isUpdate) {
            Matiere loadedMatiere = matiereService.get(matiereForm.getId());
            Matiere matiere = MatiereFormMapping.mappingFormToDTO(matiereForm, loadedMatiere);
            matiereService.update(matiere);
        } else {
            Matiere matiere = MatiereFormMapping.mappingFormToDTO(matiereForm);
            matiereService.save(matiere);
        }

        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lenregistrement")));
        saveMessages(request, actionMessages);

        matiereForm.setId(null);
        matiereForm.setNom(null);
        matiereForm.setIsUpdate(false);

        addCategoryList(request);

        return mapping.findForward("successSave");
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        MatiereService matiereService = this.getServiceLocator().getMatiereService();
        Long id = ((MatiereForm) form).getId();
        matiereService.deleteById(id);
        ActionMessages actionMessages = new ActionMessages();
        actionMessages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("success.do", getMessage(request,
                "libelle.lasuppression")));
        saveMessages(request, actionMessages);

        addCategoryList(request);

        return mapping.findForward("successDelete");
    }

    public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        Matiere matiere = MatiereFormMapping.mappingFormToDTO((MatiereForm) form);
        MatiereService matiereService = this.getServiceLocator().getMatiereService();
        List<Matiere> list = matiereService.searchByExample(matiere);
        request.setAttribute("RESULTS_LIST", list);
        return mapping.findForward("successSearch");
    }
}
